package com.woniuxy.producer;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 生产者
 */
@Component
public class OrderProducer {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 延迟队列发消息
     * @param map 应包含 cid：客户id；oid：订单id
     */
    public void send(Map<String, Integer> map) {
        amqpTemplate.convertAndSend("delay_direct_exchange", "orders", map, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(10000);
                return message;
            }
        });
    }
}
